using System;
using System.Collections.Generic;
using Matrix.Notification.Domain.Shared.Notifications;
using Matrix.Notification.Application.Contracts.Dto;

namespace Matrix.Notification.Notifications
{
    /// <summary>
    /// 通知统计DTO
    /// </summary>
    public class NotificationStatisticsDto
    {
        /// <summary>
        /// 统计时间范围
        /// </summary>
        public DateTimeRangeDto TimeRange { get; set; } = new();

        /// <summary>
        /// 总通知数
        /// </summary>
        public int TotalCount { get; set; }

        /// <summary>
        /// 按类型统计
        /// </summary>
        public Dictionary<NotificationType, int> CountByType { get; set; } = new();

        /// <summary>
        /// 按状态统计
        /// </summary>
        public Dictionary<NotificationStatus, int> CountByStatus { get; set; } = new();

        /// <summary>
        /// 按渠道统计
        /// </summary>
        public Dictionary<NotificationChannel, int> CountByChannel { get; set; } = new();

        /// <summary>
        /// 按优先级统计
        /// </summary>
        public Dictionary<NotificationPriority, int> CountByPriority { get; set; } = new();

        /// <summary>
        /// 已发送数量
        /// </summary>
        public int SentCount { get; set; }

        /// <summary>
        /// 已送达数量
        /// </summary>
        public int DeliveredCount { get; set; }

        /// <summary>
        /// 失败数量
        /// </summary>
        public int FailedCount { get; set; }

        /// <summary>
        /// 已读数量
        /// </summary>
        public int ReadCount { get; set; }

        /// <summary>
        /// 未读数量
        /// </summary>
        public int UnreadCount { get; set; }

        /// <summary>
        /// 成功率
        /// </summary>
        public double SuccessRate { get; set; }

        /// <summary>
        /// 读取率
        /// </summary>
        public double ReadRate { get; set; }

        /// <summary>
        /// 平均送达时间（毫秒）
        /// </summary>
        public double AverageDeliveryTimeMs { get; set; }

        /// <summary>
        /// 平均读取时间（毫秒）
        /// </summary>
        public double AverageReadTimeMs { get; set; }

        /// <summary>
        /// 统计生成时间
        /// </summary>
        public DateTime GeneratedAt { get; set; } = DateTime.UtcNow;
    }

    /// <summary>
    /// 用户通知统计DTO
    /// </summary>
    public class UserNotificationStatisticsDto
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public Guid UserId { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; } = string.Empty;

        /// <summary>
        /// 统计时间范围
        /// </summary>
        public DateTimeRangeDto TimeRange { get; set; } = new();

        /// <summary>
        /// 总通知数
        /// </summary>
        public int TotalCount { get; set; }

        /// <summary>
        /// 未读数量
        /// </summary>
        public int UnreadCount { get; set; }

        /// <summary>
        /// 已读数量
        /// </summary>
        public int ReadCount { get; set; }

        /// <summary>
        /// 按类型统计
        /// </summary>
        public Dictionary<NotificationType, int> CountByType { get; set; } = new();

        /// <summary>
        /// 按优先级统计
        /// </summary>
        public Dictionary<NotificationPriority, int> CountByPriority { get; set; } = new();

        /// <summary>
        /// 按渠道统计
        /// </summary>
        public Dictionary<NotificationChannel, int> CountByChannel { get; set; } = new();

        /// <summary>
        /// 读取率
        /// </summary>
        public double ReadRate { get; set; }

        /// <summary>
        /// 最后接收通知时间
        /// </summary>
        public DateTime? LastNotificationTime { get; set; }

        /// <summary>
        /// 最后阅读时间
        /// </summary>
        public DateTime? LastReadTime { get; set; }

        /// <summary>
        /// 统计生成时间
        /// </summary>
        public DateTime GeneratedAt { get; set; } = DateTime.UtcNow;
    }

    /// <summary>
    /// 通知清理结果DTO
    /// </summary>
    public class NotificationCleanupResultDto
    {
        /// <summary>
        /// 清理类型
        /// </summary>
        public NotificationCleanupType CleanupType { get; set; }

        /// <summary>
        /// 清理的通知数量
        /// </summary>
        public int CleanedCount { get; set; }

        /// <summary>
        /// 跳过的通知数量
        /// </summary>
        public int SkippedCount { get; set; }

        /// <summary>
        /// 失败数量
        /// </summary>
        public int FailedCount { get; set; }

        /// <summary>
        /// 清理耗时（毫秒）
        /// </summary>
        public long CleanupTimeMs { get; set; }

        /// <summary>
        /// 清理时间
        /// </summary>
        public DateTime CleanupTime { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 错误信息
        /// </summary>
        public List<string> Errors { get; set; } = new();

        /// <summary>
        /// 警告信息
        /// </summary>
        public List<string> Warnings { get; set; } = new();
    }

    /// <summary>
    /// 通知导入结果DTO
    /// </summary>
    public class NotificationImportResultDto
    {
        /// <summary>
        /// 导入的总数量
        /// </summary>
        public int TotalCount { get; set; }

        /// <summary>
        /// 成功导入数量
        /// </summary>
        public int SuccessCount { get; set; }

        /// <summary>
        /// 失败数量
        /// </summary>
        public int FailedCount { get; set; }

        /// <summary>
        /// 跳过数量（重复等）
        /// </summary>
        public int SkippedCount { get; set; }

        /// <summary>
        /// 导入耗时（毫秒）
        /// </summary>
        public long ImportTimeMs { get; set; }

        /// <summary>
        /// 导入时间
        /// </summary>
        public DateTime ImportTime { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 失败的记录
        /// </summary>
        public List<ImportErrorDto> Errors { get; set; } = new();

        /// <summary>
        /// 警告信息
        /// </summary>
        public List<string> Warnings { get; set; } = new();
    }

    /// <summary>
    /// 导入错误DTO
    /// </summary>
    public class ImportErrorDto
    {
        /// <summary>
        /// 行号
        /// </summary>
        public int RowNumber { get; set; }

        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage { get; set; } = string.Empty;

        /// <summary>
        /// 错误详情
        /// </summary>
        public string? ErrorDetails { get; set; }

        /// <summary>
        /// 原始数据
        /// </summary>
        public string? OriginalData { get; set; }
    }

    /// <summary>
    /// 通知验证结果DTO
    /// </summary>
    public class NotificationValidationResultDto
    {
        /// <summary>
        /// 是否有效
        /// </summary>
        public bool IsValid { get; set; }

        /// <summary>
        /// 验证错误
        /// </summary>
        public List<ValidationErrorDto> Errors { get; set; } = new();

        /// <summary>
        /// 验证警告
        /// </summary>
        public List<string> Warnings { get; set; } = new();

        /// <summary>
        /// 验证耗时（毫秒）
        /// </summary>
        public long ValidationTimeMs { get; set; }

        /// <summary>
        /// 验证时间
        /// </summary>
        public DateTime ValidationTime { get; set; } = DateTime.UtcNow;
    }

    /// <summary>
    /// 验证错误DTO
    /// </summary>
    public class ValidationErrorDto
    {
        /// <summary>
        /// 字段名
        /// </summary>
        public string FieldName { get; set; } = string.Empty;

        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage { get; set; } = string.Empty;

        /// <summary>
        /// 错误代码
        /// </summary>
        public string? ErrorCode { get; set; }

        /// <summary>
        /// 错误级别
        /// </summary>
        public ValidationLevel Level { get; set; } = ValidationLevel.Error;
    }

    /// <summary>
    /// 验证级别
    /// </summary>
    public enum ValidationLevel
    {
        Info = 1,
        Warning = 2,
        Error = 3,
        Critical = 4
    }

    /// <summary>
    /// 通知预览DTO
    /// </summary>
    public class NotificationPreviewDto
    {
        /// <summary>
        /// 预览内容
        /// </summary>
        public string PreviewContent { get; set; } = string.Empty;

        /// <summary>
        /// HTML预览内容
        /// </summary>
        public string? HtmlPreviewContent { get; set; }

        /// <summary>
        /// 预览主题
        /// </summary>
        public string? PreviewSubject { get; set; }

        /// <summary>
        /// 使用的变量
        /// </summary>
        public Dictionary<string, object> UsedVariables { get; set; } = new();

        /// <summary>
        /// 缺失的变量
        /// </summary>
        public List<string> MissingVariables { get; set; } = new();

        /// <summary>
        /// 预估长度
        /// </summary>
        public int EstimatedLength { get; set; }

        /// <summary>
        /// 预估发送时间（毫秒）
        /// </summary>
        public long EstimatedSendTimeMs { get; set; }

        /// <summary>
        /// 是否通过内容检查
        /// </summary>
        public bool PassesContentCheck { get; set; } = true;

        /// <summary>
        /// 内容检查结果
        /// </summary>
        public List<string> ContentCheckResults { get; set; } = new();

        /// <summary>
        /// 预览生成时间
        /// </summary>
        public DateTime GeneratedAt { get; set; } = DateTime.UtcNow;
    }

    /// <summary>
    /// 复制通知DTO
    /// </summary>
    public class DuplicateNotificationDto
    {
        /// <summary>
        /// 新标题
        /// </summary>
        public string? NewTitle { get; set; }

        /// <summary>
        /// 新接收者ID
        /// </summary>
        public Guid? NewRecipientId { get; set; }

        /// <summary>
        /// 新接收者邮箱
        /// </summary>
        public string? NewRecipientEmail { get; set; }

        /// <summary>
        /// 新接收者手机号
        /// </summary>
        public string? NewRecipientPhoneNumber { get; set; }

        /// <summary>
        /// 新调度时间
        /// </summary>
        public DateTime? NewScheduledTime { get; set; }

        /// <summary>
        /// 是否复制附件
        /// </summary>
        public bool CopyAttachments { get; set; } = true;

        /// <summary>
        /// 是否复制参数
        /// </summary>
        public bool CopyParameters { get; set; } = true;

        /// <summary>
        /// 新参数
        /// </summary>
        public Dictionary<string, object>? NewParameters { get; set; }
    }
}